﻿@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@using FormBuilder.Helpers
@using FormBuilder.Tailwindcss
@using Microsoft.Extensions.Options
@using FormBuilder.Components
@using FormBuilder.Components.Workflow
@using FormBuilder
@using Microsoft.AspNetCore.Http;
@inject IOptions<FormBuilderOptions> options
@inject IHttpContextAccessor HttpContextAccessor;
@inject IUriProvider uriProvider
@model FormBuilder.UIs.WorkflowViewModel

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
    var antiforgeryToken = HttpContextAccessor.HttpContext.Request.Cookies[options.Value.AntiforgeryCookieName];
    Model.AntiforgeryToken.CookieValue = antiforgeryToken;
    var step = Model.Workflow.Steps.Single(s => s.Id == Model.CurrentStepId);
}

<component type="typeof(WorkflowViewer)" 
    render-mode="ServerPrerendered" 
    param-FormRecords="@Model.FormRecords"
    param-Workflow="@Model.Workflow"
    param-Input="@Model.Input"
    param-AntiforgeryToken="@Model.AntiforgeryToken"
    param-CurrentStepId="@Model.CurrentStepId" 
    param-ErrorMessages="@Model.ErrorMessages" 
    param-SuccessMessages="@Model.SuccessMessages"
    param-Template="@Model.Template"/>

@section Header {
    @foreach(var cssStyle in Model.Template.CssStyles)
    {
        <link rel="stylesheet" href="@uriProvider.GetCssUrl(Model.Template.Id, cssStyle)" />
    }

    @foreach (var jsStyle in Model.Template.JsStyles)
    {
        <script src="@uriProvider.GetJsUrl(Model.Template.Id, jsStyle)" type="text/javascript"></script>
    }
}

@section Scripts {
    <script type="text/javascript">        
        let csharpReference;
        var isInitialized  = false;
        var init = function() {

            var tryListenForm = function () {
                const elt = $("#mobileAuthForm");
                if (isInitialized === true) return;
                if (elt.length === 0) {
                    setTimeout(() => tryListenForm(), 500);
                    return;
                }

                isInitialized = true;
                elt.submit(function (e) {
                    e.preventDefault();
                    csharpReference.invokeMethodAsync("Navigate", "mobileAuthFormId", "Url", "https://upload.wikimedia.org/wikipedia/commons/thumb/2/2f/Google_2015_logo.svg/1200px-Google_2015_logo.svg.png");
                });
            }

            tryListenForm();
        }

        var setCsharpReference = function (ref) {
            csharpReference = ref;
            init();
        };
    </script>
}